package com.st.mems_gesture.composable

import androidx.compose.animation.animateColorAsState
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.st.blue_sdk.features.mems_gesture.MemsGestureType
import com.st.mems_gesture.MemsGestureViewModel
import com.st.mems_gesture.R
import com.st.ui.theme.LocalDimensions
import com.st.ui.theme.PrimaryYellow
import com.st.ui.theme.Shapes


@Composable
fun MemsGestureDemoContent(
    modifier: Modifier,
    viewModel: MemsGestureViewModel
) {
    val gestureData by viewModel.gestureData.collectAsStateWithLifecycle()

    Box(
        modifier = modifier.fillMaxSize(),
        contentAlignment = Alignment.Center
    ) {

        val glanceImage by remember(key1 = gestureData.second) {
            derivedStateOf { gestureData.first.gesture.value == MemsGestureType.Glance }
        }

        val pickUpImage by remember(key1 = gestureData.second) {
            derivedStateOf { gestureData.first.gesture.value == MemsGestureType.PickUp }
        }

        val wakeUpImage by remember(key1 = gestureData.second) {
            derivedStateOf { gestureData.first.gesture.value == MemsGestureType.WakeUp }
        }

        Column(
            modifier = modifier.fillMaxWidth().padding(start = LocalDimensions.current.paddingLarge),
            horizontalAlignment = Alignment.Start,
            verticalArrangement = Arrangement.SpaceAround
        ) {
            MemsGestureItem(
                modifier = Modifier.weight(1f),
                isActive = glanceImage,
                id = R.drawable.mems_gesture_glance,
                label = "Glance"
            )

            MemsGestureItem(
                modifier = Modifier.weight(1f),
                isActive = pickUpImage,
                id = R.drawable.mems_gesture_pick_up,
                label = "Pick Up"
            )

            MemsGestureItem(
                modifier = Modifier.weight(1f),
                isActive = wakeUpImage,
                id = R.drawable.mems_gesture_wake_up,
                label = "Wake Up"
            )
        }

        if (gestureData.second == null) {
            Text(
                style = MaterialTheme.typography.displayMedium,
                text = "Waiting data…"
            )
        }
    }
}